package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.LostAndFound.domain.Article;
import com.LostAndFound.domain.FoundArticle;
import com.LostAndFound.service.MyIssueService;
import com.LostAndFound.utils.CommonsUtils;

public class MyIssueServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	MyIssueService service = new MyIssueService();
	// 展示个人全部发布的记录
	public void getMyIssueArticleList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uid = request.getParameter("uid");
		List<Article> articleList = null;
		articleList = service.getMyIssueArticleList(uid);
		HttpSession session = request.getSession();
		session.setAttribute("myIssueArticleList", articleList);
		
		response.setContentType("text/html;charset=utf-8");
		response.sendRedirect("user/03/myIssue.jsp");
	}
	// 展示个人发布的记录详情
	public void showMyIssueArticleDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		String foundFlag = request.getParameter("foundFlag");
		String invalidFlag = request.getParameter("invalidFlag");
		Map<String, Object> article = null;
		article = service.showMyIssueArticleDetail(aid,foundFlag,invalidFlag);
		HttpSession session = request.getSession();
		session.setAttribute("article", article);
		response.sendRedirect(request.getContextPath()+"/user/03/myIssueDetail.jsp");
	}
	//delete article
	public void handleDeleteArticleByAid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		boolean success = service.handleDeleteArticleByAid(aid);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	// 确认领取
	public void confirmOrderArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		//String uid = request.getParameter("uid");
		String foundInfo = request.getParameter("foundInfo");
		// 封装对象
		FoundArticle foundArticle = new FoundArticle();
		Article article = new Article();
		article.setAid(aid);
		foundArticle.setArticle(article);
		foundArticle.setFoundTime(CommonsUtils.getCurrentFormatTime());
		foundArticle.setFoundInfo(foundInfo);

		boolean success = service.confirmOrderArticle(foundArticle);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}